/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *
 * @author aeguzkiza and Espron
 */
public class DB2Hash extends Hashtable<String, String>{
    public DB2Hash() {}
    public DB2Hash(ResultSet rs) throws SQLException, Exception {
        //MySQL_Query DB = new MySQL_Query();
        //ResultSet rs = DB.SQLquery(query);
        ResultSetMetaData metadata = rs.getMetaData();
        int n = metadata.getColumnCount();
        int currentRow = rs.getRow();
        rs.last();
        int rowNumber = rs.getRow();    // Gets row number on DB selected table.
        //rs.first();
        rs.absolute(currentRow);    // Recovers the row selected at the start of the function.
            for (int i = 0; i < n; i++ ) {
                String cname = metadata.getColumnName(i+1);
                String value = null;
                if(rowNumber != 0){value = rs.getString(i+1);}  // If there's no data row, assigns null value to each DB header.
                /*if(cname.equals("Id")){ // Exception case for Id variable.
                    String tableName = metadata.getTableName(i+1);
                    cname = tableName+"."+cname;
                }*/
                //System.out.println(cname+" : "+value);
                put(cname, (value == null ? "" : value) );
            }
        //}
    }
    public void set(String key, String value) {
        this.put(key, (value == null ? "" : value) );
    }
    public String printSQLinsert(String tableName) {    // UNUSED
        ArrayList var = new ArrayList();
        Enumeration<String> keys = keys();
        Enumeration<String> values = elements();
        String query = null;
        String k = null;
        String q = null;
        ArrayList v = null;
        for(int i=0;i<size();i++){
            if (k != null) {
                k += ", ";
                q += ", ";
            }
            else {
                k = "";
                q = "";
            }

            k += keys.nextElement();
            q += "?";
            v.add("'" + values.nextElement() + "'");
        }
        query = "INSERT INTO "+tableName+" (" + k + ") VALUES (" + q + ")";
        
        return query;
        //return "INSERT INTO "+tableName+" (" + k + ") VALUES (" + v + ")";
    }
    public String questionSQLinsert(){
        Enumeration<String> keys = keys();
        String q = null;
        for(int i=0;i<size();i++){
            if (q != null) {
                q += ", ";
            }
            else {
                q = "";
            }

            q += "?";
        }
        return q;
    }
    public String keysSQLinsert(){    
        Enumeration<String> keys = keys();
        String k = null;
        for(int i=0;i<size();i++){
            if (k != null) {
                k += ", ";
            }
            else {
                k = "";
            }

            k += keys.nextElement();
        }
        return k;
    }
    public String keysSQLupdate(){
        Enumeration<String> keys = keys();
        String k = null;
        for(int i=0;i<size();i++){
            if (k != null) {
                k += ", ";
            }
            else {
                k = "";
            }

            k += keys.nextElement()+"=?";
        }
        return k;
    }
    public ArrayList valuesSQLinsert(){  
        Enumeration<String> values = elements();
        ArrayList v = new ArrayList();
        for(int i=0;i<size();i++){
            v.add(values.nextElement());
        }
        return v;
    }
}
